package sp.util.redis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 序列化工具类  
 * @description: 将序列化到redis中  以字节流数组形式
 * @author 浮~沉
 * @date 2020年5月13日
 */
public class SerializeUtil {

	public static byte[] serialize(Object object) {
		ByteArrayOutputStream byteArrayOutputStream = null;
		ObjectOutputStream objectOutputStream = null;
		try {
			byteArrayOutputStream = new ByteArrayOutputStream();
			objectOutputStream  = new ObjectOutputStream(byteArrayOutputStream);
			objectOutputStream.writeObject(object);
			return  byteArrayOutputStream.toByteArray();
		} catch (IOException e) {
			throw new RuntimeException("illeage set such as object:" +object);
		}
	}
	
	
	public static Object unSerialize(byte[] arrayByte) {
		ByteArrayInputStream byteArrayInputStream = null;
		ObjectInputStream objectInputStream = null;
		try {
			byteArrayInputStream = new ByteArrayInputStream(arrayByte);
			objectInputStream = new ObjectInputStream(byteArrayInputStream);
			return objectInputStream.readObject();
		} catch (Exception e) {
			throw new RuntimeException("warning! can't get such as object");
		}
		
	}
	
}
